<?php
session_start();
//we initialize flight conditions here with php in order to eventually get the variables from url
// Default : 'Dent de Crolles, heading East'
$takeoff_lat = 45.3089;
$takeoff_lng = 5.85639;
$takeoff_heading = 90;
$winch_altitude = 0;

// flight conditions available from form ?
if (isset($_POST['takeoff_lat'])) $takeoff_lat = $_POST['takeoff_lat'];
if (isset($_POST['takeoff_lng'])) $takeoff_lng = $_POST['takeoff_lng'];
if (isset($_POST['takeoff_heading'])) $takeoff_heading = $_POST['takeoff_heading'];
if (isset($_POST['winch_altitude'])) $winch_altitude = $_POST['winch_altitude'];

$wind_speed = 4;
$wind_direction = $takeoff_heading;
if (isset($_POST['wind_speed'])) $wind_speed = $_POST['wind_speed'];
if (isset($_POST['wind_direction'])) $wind_direction = $_POST['wind_direction'];
if (isset($_POST['wind_direction'])) $takeoff_heading = $_POST['wind_direction'];

$thermal_density=2;
$thermal_height=1800;
$thermal_speed=5;
$thermal_size=70;
if (isset($_POST['thermal_density'])) $thermal_density = $_POST['thermal_density'];
if (isset($_POST['thermal_height'])) $thermal_height = $_POST['thermal_height'];
if (isset($_POST['thermal_speed'])) $thermal_speed = $_POST['thermal_speed'];
if (isset($_POST['thermal_size'])) $thermal_size = $_POST['thermal_size'];

$temperature=21;
if (isset($_POST['temperature'])) $temperature = $_POST['temperature'];

$glider = "4e01a5a1f374d9d3a426bad1ca64772";
if (isset($_POST['glider'])) $glider = $_POST['glider'];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>pgSim, a paragliding simulator</title>
  <link rel=stylesheet href='style/style.css' type='text/css' />

  <script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=ABQIAAAAloePAlEicH6IDTkMBLcJrhQLYAuJ7-mOmkKXyFUcffg-VgEESRR4-l83zYg5za2MJV3QOc3a2UdjSw" type="text/javascript"></script>
  <script src="http://www.google.com/jsapi?key=ABQIAAAAloePAlEicH6IDTkMBLcJrhQLYAuJ7-mOmkKXyFUcffg-VgEESRR4-l83zYg5za2MJV3QOc3a2UdjSw" type="text/javascript"></script>



  <script type="text/javascript">

	// FLIGHT CONDITIONS INITIALISED HERE FOR THE WHOLE APP AS 'GLOBAL' VARIABLES //
	var PILOT_NAME = '<?php echo $_POST['pilot_name'];?>'; 
	var SITE_NAME = '<?php echo $_POST['site_name'];?>'; 

	var TAKEOFF_LAT = <?php echo $takeoff_lat;?>;
	var TAKEOFF_LNG = <?php echo $takeoff_lng;?>;
	var TAKEOFF_HEADING = <?php echo $takeoff_heading;?>;
	var WINCH_ALTITUDE = <?php echo $winch_altitude;?>;

	var WIND_SPEED = <?php echo $wind_speed;?>;
	var WIND_DIRECTION = <?php echo $wind_direction;?>;
	 
	var THERMAL_VZ_MAX = <?php echo $thermal_speed;?>;
	var THERMAL_TOP = <?php echo $thermal_height;?>;
	var THERMAL_DENSITY = <?php echo $thermal_density;?>;
	var THERMAL_RADIUS = <?php echo $thermal_size;?>;

	var temperatureInit = <?php echo $temperature;?>;

	var GLIDER = '<?php echo $glider;?>';

	google.load("earth", "1");
	google.load("maps", "2.99");

	var ge = null;
	var geocoder;
	var truck;

	function el(e) { return document.getElementById(e); }

	function Sample(description, url) {
	  this.description = description;
	  this.url = url;
	  return this;
	}

	var samples = [];

	function init() {
	  geocoder = new GClientGeocoder();

	  init3D();

	  var map2d = new GMap2(document.getElementById("map2d"));
	  map2d.setCenter(new GLatLng(<?php echo $takeoff_lat;?>, <?php echo $takeoff_lng;?>), 13);

	}

	function initCallback(object) {
	  ge = object;
	  ge.getWindow().setVisibility(true);
	  ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, true);
	  ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT);

	  truck = new Truck();
	}

	function failureCallback(err) {
	  /***
	   * This function will be called if plugin fails to load, in case
	   * you need to handle that error condition.
	   ***/
	}

	function init3D() {
	  google.earth.createInstance("map3d", initCallback, failureCallback);
	}

	function submitLocation() {
	  doGeocode(el('address').value);
	}

	function doGeocode(address) {
	  geocoder.getLatLng(address, function(point) {
	    if (point) {
	      if (ge != null && truck != null) {
		truck.teleportTo(point.y, point.x);
	      }
	    }
	  });
	}
	
function resizeDiv(divId, size){
	var divHeight = parseInt(document.getElementById(divId).style.height);
//	document.write(divHeight + "popopo<br/>");
	var divWidth = parseInt(document.getElementById(divId).style.width);
	if (size == 'bigger'){
		document.getElementById(divId).style.height = Math.round(1.1*divHeight) + "px";
		document.getElementById(divId).style.width = Math.round(1.1*divWidth) + "px";
	}
	if (size == 'smaller'){
//		document.write(Math.round(divHeight/1.2) + "px");
		document.getElementById(divId).style.height = Math.round(divHeight/1.1) + "px";
		document.getElementById(divId).style.width = Math.round(divWidth/1.1) + "px";
	}
}
 
function hideDiv(divId){
	document.getElementById(divId).style.display = 'none';
}
 
function showDiv(divId){
	document.getElementById(divId).style.display = 'block';
}

  </script>

  <script type="text/javascript" src="math3d.js"></script>
  <script type="text/javascript" src="paraglide.js"></script>
  <script type="text/javascript" src="sound/script/soundmanager2.js"></script>
  <script type="text/javascript">
	// VARIO SOUND
	soundManager.url = 'sound/soundmanager2.swf'; // override default SWF url
	soundManager.debugMode = false;
	soundManager.consoleOnly = false;
	soundManager.onload = function() {
	  soundManager.createSound({
	    id: 'beep',
	    url: 'sound/beep.mp3',
	    autoLoad: true
	  });  
	}
	// VARIO SOUND
  </script>
</head>

<body onload='init();' onunload="GUnload()"; onKeyDown="return keyDown(event);" onKeyUp="return keyUp(event);" >
<center>
<table>
 <tr>
  <td valign="top">
    <div id="GPS" style="background:url('img/gpsBackground.jpg'); width:276px; height:520px">
       <div id='GPSscreen' style="width: 170px; height: 220px; border: solid 1px gray; position:relative; left:52px; top:136px">
         <div id='mapGPS' style='width: 170px; height: 220px; ' >
         </div>
         <div style='position:relative; bottom:118px; width:170px; height:16px'><center><img  id="gpsArrow" src="img/arrow_down.png" name="headingImg" width="16" height="16" /></center>
	 </div>
         <div id='GPSinfo' style='position:relative; bottom:80px'>
         </div>
       </div>
    </div>
  </td>
  <td valign="top">
  <div id='map3d_container' style='width: 762px; height: 467px;border: solid 1px black'>
    <div id='map3d' style='width: 760px; height: 465px;border: solid 1px gray'></div>
    <div id='endFlight'></div>
    <div id='igc' style='display:none'>&nbsp;<br />
	<!--
      <form name="igcForm" action="view_flight.php" method="post">
        <textarea name='igcFile' style='width: 650px; height: 180px; display:none'></textarea>
        <input name='member_id' type="hidden" value="<?php echo $_SESSION['id_membre'];?>" />
        <input name='site_id' type="hidden" value="<?php echo $_SESSION['site'];?>" />
        <input name='wind_speed' type="hidden" value="<?php echo $wind_speed;?>" />
        <input name='wind_direction' type="hidden" value="<?php echo $wind_direction;?>" />
        <input name='thermal_height' type="hidden" value="<?php echo $thermal_height;?>" />
        <input name='thermal_size' type="hidden" value="<?php echo $thermal_size;?>" />
        <input name='thermal_density' type="hidden" value="<?php echo $thermal_density;?>" />
        <input name='thermal_speed' type="hidden" value="<?php echo $thermal_speed;?>" />
        <input type="submit" value="view (and eventually save) this flight" />
      </form>
	-->  
    </div>
  </div>
    <center>
      <div id="takeOffButton">
        &nbsp;<br />
        <input type=button class="button medw" onmousedown="leftButtonDown=true;" onmouseup="leftButtonDown=false;" value="left">
        <input type=button class="button medw" onmousedown="takeoffButtonDown=true;" onmouseup="takeoffButtonDown=false;" value="Run !">
         <input type=button class="button medw" onmousedown="rightButtonDown=true;" onmouseup="rightButtonDown=false;" value="right"><br />&nbsp;
		 &nbsp;<br />If you are stuck at take off, try reload the page (try even several times, sometimes it helps...)<br />
		 To change view during flight, use 's', 'd', 'x', 'f', 'z', 'a' keys : see 'pg sim how-to' link for more details
<!--		 <input type=button class="button medw" onmousedown="takeoffCheatButtonDown=true;" onmouseup="takeoffCheatButtonDown=false;" value="(-TESTING -click here if you are stuck at take off)">
-->      </div>
      <div id="pilotButtons" style="display:none">
    <table>
      <tr>
        <td></td>
        <td><input type=button class="button medw" onmousedown="gasButtonDown=true;" onmouseup="gasButtonDown = false;" value="speed bar"></td>
        <td></td>
      </tr>
      <tr>
        <td>
        <input type=button class="button medw" onmousedown="hardLeftButtonDown=true;" onmouseup="hardLeftButtonDown=false;" value="hard left">
        <input type=button class="button medw" onmousedown="leftButtonDown=true;" onmouseup="leftButtonDown=false;" value="left"></td>
        <td></td>
        <td><input type=button class="button medw" onmousedown="rightButtonDown=true;" onmouseup="rightButtonDown=false;" value="right">
        <input type=button class="button medw" onmousedown="hardRightButtonDown=true;" onmouseup="hardRightButtonDown=false;" value="hard right">
        </td>
      </tr>
      <tr>
        <td></td>
        <td align="center"><input type=button class="button medw" onmousedown="reverseButtonDown=true;" onmouseup="reverseButtonDown=false;" value="brake"></td>
        <td></td>
      </tr>
    </table>
      </div>

    </center>
  </td>
  
  <td  valign="top" align="center">
     <div  id="varioTableCell"> 
      <MAP name="map">
        <AREA shape="circle" coords="122,272,12" href="#" onmousedown="javascript:varioSoundOnOff=switchTrueFalse(varioSoundOnOff);">
      </MAP>
   <div id="vario"  style="position:relative; width: 240px; height: 400px; background:url(img/varioScreenBackground.jpg); overflow:hidden; margin:0px; padding:0px">
      <div id="varioBarSpacer" style="height:150px;  margin:0px; padding:0px"></div>
      <div id="varioBar" style="background:black; height:1px;  margin:0px; padding:0px"></div>
      <div id="varioImg" style="position:absolute; top:0px; left:0px">
        <img src="img/vario.png" USEMAP="#map" border="0" />
        <div id="varioClimbingRate" style="position:absolute; width:60px; top:75px; right:65px;  background:url(img/varioScreenBackground.jpg); text-align:right">
             <font face="Comic Sans MS" color="#000000" size=4> ____ </font>
        </div>
        <div id="varioAltitude" style="position:absolute; width:50px; top:124px; right:58px;  background:url(img/varioScreenBackground.jpg); text-align:right">
             <font face="Comic Sans MS" color="#000000" size=5> ____ </font>
        </div>
        <div id="varioTemperature" style="position:absolute; top:156px; right:92px;  background:url(img/varioScreenBackground.jpg); text-align:right">
             <font face="Comic Sans MS" color="#000000" size=5> __ </font>
        </div>
      </div>
    </div>

<!--
    <div id="jsInfo" >Js info</div>
    <div id="jsInfo2" style="background-color: gainsboro; border: solid 1px gray; width:200px;" ></div>
-->

   <div id="jsInfo3" ></div>
    <div>
      <div id="compass_container" style="position:relative;top:0px;right:0px;height:40px;width:200px;overflow:hidden;border: solid 1px gray">
        <div id="compass_image" style="height:40px;width:1080px;background:url(img/compass_ruler.jpg);position:relative;top:0px;right:<?php echo 2*$_POST['takeoff_heading']+110;?>px">
        </div>
      </div>
    </div>
    
    <div>
      <a href="how_to.html" target="_blank"><img src="img/help.png" border="0" /><em>pg sim how-to</em></a>
    </div>
 	</div> 
	<div id="instrumentText" style="display:none;text-align:left;"></div> 
 </td>
 </tr>
</table>
	Window size : 
<a href="javascript:resizeDiv('map3d', 'bigger');resizeDiv('map3d_container', 'bigger');"><img src="img/magnifier_zoom_in.png" border="0" title="bigger" /></a>&nbsp;/&nbsp;
<a href="javascript:resizeDiv('map3d', 'smaller');resizeDiv('map3d_container', 'smaller');"><img src="img/magifier_zoom_out.png" border="0" title="smaller" /></a> 
	<br /> 
		Instruments :
<a href="javascript:hideDiv('GPS');hideDiv('gpsArrow');hideDiv('varioTableCell');showDiv('instrumentText');"><img src="img/calculator_delete.png" border="0" title="hide instruments" /></a>&nbsp;/&nbsp;
<a href="javascript:showDiv('GPS');showDiv('gpsArrow');showDiv('varioTableCell');hideDiv('instrumentText');"><img src="img/calculator_add.png" border="0" title="show instruments" /></a> 

</center>


<script type="text/javascript">
// Display the map, with some controls and set the initial location 
      var mapobj = document.getElementById("mapGPS");
      var mapGPS = new GMap2(mapobj);
      mapGPS.addControl(new GSmallMapControl());
	  mapGPS.addControl(new google.maps.MenuMapTypeControl());
 //     mapGPS.addControl(new GScaleControl() ) ;
      mapGPS.setCenter(new GLatLng(<?php echo $takeoff_lat.",".$takeoff_lng; ?>), 13, G_NORMAL_MAP);
</script>


 <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
 </script>
 <script type="text/javascript">
  _uacct = "UA-1083947-1";
  urchinTracker();
  olLoaded=1;
 </script>

<script type="text/javascript" src="wz_rotateimg.js"></script>
<script type="text/javascript">
<!--
SET_ROTATABLE("headingImg",<?php echo $takeoff_heading?>);
//-->
</script> 
</body>
</html>
